package com.demo.test;
import java.sql.*;

/**封装数据库连接和关闭*/
public class JDBCUtils {
    public static Connection getConnection() throws Exception{
        //读取配置文件基本信息
      /*  InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);


        String user = pros.getProperty("root");
        String password = pros.getProperty("BigData.2018v6");
        String url = pros.getProperty("jdbc:mysql://localhost:3306/cloud");
        String driverClass = pros.getProperty("com.mysql.cj.jdbc.Driver");
*/

        String  driverClass="com.mysql.cj.jdbc.Driver";

        String url = "jdbc:mysql://localhost:3336/cloud";
        String user = "root";
        String password = "BigData.2018v6";

        //加载驱动
        Class.forName(driverClass);
        //获取连接
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;
    }
    public static void closeResource(Connection conn, Statement ps){
        try {
            if(ps != null)
                ps.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        try {
            if(conn != null)
                conn.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public static void closeResource(Connection conn, Statement ps, ResultSet rs){
        try {
            if(ps != null)
                ps.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        try {
            if(conn != null)
                conn.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
        try {
            if(rs!=null)
                rs.close();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}